﻿#ifndef __HJFRAME_H__
#define __HJFRAME_H__

#include <fcntl.h>

#define UPGRADE_EASE_CMD    0x80
#define UPGRADE_WRITE_CMD   0x81
#define UPGRADE_READ_CMD    0x82
#define UPGRADE_REBOOT_CMD  0x83
#define UPGRADE_READ_VERSION  0x84

#define JSON_TRANSFER_CMD   0x0100

#define ACK_MASK            0x8000

class Frame {

public:

    uint8_t     head1;
    uint8_t     head2;
    uint16_t    len;
    uint8_t     src_addr[4];
    uint8_t     des_addr[4];
    uint16_t    cmd;
    uint8_t     paload[1024];
    uint16_t    paload_len;
    uint32_t    crc32;
    uint8_t     end1;
    uint8_t     end2;

    void make_ease_pack(uint8_t* src_addr, uint8_t* des_addr);

    void make_reboot_pack(uint8_t* src_addr, uint8_t* des_addr);

    void make_read_pack(uint8_t* src_addr, uint8_t* des_addr, uint32_t read_addr, uint16_t read_len);

    void make_write_pack(uint8_t* src_addr, uint8_t* des_addr, uint32_t write_addr, uint8_t* write_data, uint16_t* write_len);

    void unmake_write_pack(uint8_t* src_addr, uint8_t* des_addr, uint32_t* write_addr, uint8_t** write_data, uint16_t* write_len);

    void unmake_read_pack(uint8_t* src_addr, uint8_t* des_addr, uint32_t* read_addr, uint16_t* read_len);

    //组包 包->数组
    void make_pack(uint8_t* buff, uint16_t* len);

    //解包 数组->包
    void unmake_pack(uint8_t* buff, uint16_t* len);



};

class FrameRecv {

public:
    bool proc_one_byte(Frame* frame, uint8_t data);

private:

    uint8_t recv_buff[1024 + 20] = {0};
    uint16_t recv_len = 0;
    uint16_t recv_index = 0;
};


#endif // CANBUSPORT_H
